//
//  ZYMainViewController.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/10.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

class ZYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 自定义tabBar
        let zyTabBar = ZYTabBar()
        // 只读不能赋值
//        self.tabBar = zyTabBar
        // 使用kvc对只读属性赋值,设置自定义tabBar
        setValue(zyTabBar, forKey: "tabBar")
        
        zyTabBar.composeBtnClosure = { [weak self] in
           self?.showComposeVc()
        }
        
        zyTabBar.zyTBDelegate = self
        

//  添加主控制器的两种方式
//        viewControllers
//        addChildViewController(<#T##childController: UIViewController##UIViewController#>)
        addChildViewController(ZYHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(ZYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(ZYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ZYProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }

    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        // 设置全局统一的主题颜色，最好优先执行，可以考虑放在appdelegate中
        UITabBar.appearance().tintColor = UIColor.orangeColor()
//        ***************************
        // 设置title和image
//        childController.tabBarItem.title = title
////        childController.title = title 等于下行代码
//        childController.navigationItem.title = title
        
        // 设置统一文字
        childController.title = title//等价以上两句代码
        
        childController.tabBarItem.image = UIImage(named: imageName)
        // 修改navigationItem选中的文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        // 设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        // 添加图片并取消图片渲染效果
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //添加子控制器
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }
 

    // 进入发微博界面
    private func showComposeVc() {
        let composeVc = ZYComposeViewController()
        let composeNav = UINavigationController(rootViewController: composeVc)
        presentViewController(composeNav, animated: true, completion: nil)
    }
    
    
    
}

extension ZYMainViewController: ZYTabBarDelegate {
    
    func didSelectComposeBtn() {
        
        print("代理方法")
    }
    
}







